﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>PixelSearch - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The PixelSearch function searches a region of the screen for a pixel of the specified color." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>PixelSearch</h1>

<p>Searches a region of the screen for a pixel of the specified color.</p>

<pre class="Syntax"><span class="func">PixelSearch</span> OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID <span class="optional">, Variation, Mode</span></pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>OutputVarX/Y</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#variables">变量</a></p>
    <p>The variables in which to store the X and Y coordinates of the first pixel that matches <em>ColorID</em> (if no match is found, the variables are made blank). Coordinates are relative to the active window unless <a href="CoordMode.htm">CoordMode</a> was used to change that.</p>
  </dd>

  <dt>X1, Y1</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The X and Y coordinates of the upper left corner of the rectangle to search. <strong>Coordinates are relative to the active window unless <a href="CoordMode.htm">CoordMode</a> was used to change that</strong>.</p>
  </dd>

  <dt>X2, Y2</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The X and Y coordinates of the lower right corner of the rectangle to search. Coordinates are relative to the active window unless <a href="CoordMode.htm">CoordMode</a> was used to change that.</p>
  </dd>

  <dt>ColorID</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The color ID to search for. This is typically expressed as a hexadecimal number in red-green-blue (RGB) format. 例如: <code>0x9d6346</code>. Color IDs can be determined using Window Spy (accessible from the tray menu) or via <a href="PixelGetColor.htm">PixelGetColor</a>.</p>
  </dd>

  <dt>Variation</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>A number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of the color. This parameter is helpful if the color sought is not always exactly the same shade. If you specify 255 shades of variation, all colors will match. The default is 0 shades.</p>
  </dd>

  <dt>Mode</dt>
  <dd>
      <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
      <p>This parameter contains a string of options. Currently it can only have one value (if not empty):</p>
      <p id="slow"><strong>Slow</strong>: Uses a slower searching method that in most cases dramatically increases the amount of CPU time used by the search. If the screen's color depth is 16-bit or lower, the <em>Variation</em> parameter might behave slightly differently in fast mode than it does in slow mode. Finally, the fast mode searches the screen row by row (top down) whereas the slow mode searches column by column. Therefore, slow mode might find a different pixel than fast mode if there is more than one matching pixel.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#boolean">整数(布尔值)</a></p>
<p>This function returns 1 (true) if the color was found in the specified region, or 0 (false) if it was not found.</p>

<h2 id="Error_Handling">错误处理</h2>
<p>An exception is thrown if there was a problem that prevented the function from conducting the search.</p>

<h2 id="Remarks">备注</h2>
<p>The region to be searched must be visible; in other words, it is not possible to  search a region of a window hidden behind another window. By contrast, pixels beneath the mouse cursor can usually be detected. The exception to this is cursors in games, which in most cases will hide any pixels beneath them.</p>
<p>Although color depths as low as 8-bit (256-color) are supported, the fast mode performs much better in 24-bit or 32-bit color.</p>
<p>Slow mode only: By default, the search starts at the upper-left pixel of the region and checks all pixels vertically beneath it for a match. If no match is found there, the search continues to the right, column by column, until it finds a matching pixel. The default left-to-right search order can be inverted by swapping <em>X1</em> and <em>X2</em> in the parameter list. In other words, if <em>X1</em> is greater than <em>X2</em>, the search will be conducted from right to left, starting at column <em>X1</em>. Similarly, if <em>Y1</em> is greater than <em>Y2</em>, each column of pixels to be searched starting at the bottom rather than the top. Finally, if the region to be searched is large and the search is repeated with high frequency, it may consume a lot of CPU time. To alleviate this, keep the size of the area to a minimum.</p>
<h2 id="Related">相关</h2>
<p><a href="PixelGetColor.htm">PixelGetColor</a>, <a href="ImageSearch.htm">ImageSearch</a>, <a href="CoordMode.htm">CoordMode</a>, <a href="MouseGetPos.htm">MouseGetPos</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>if PixelSearch(Px, Py, 200, 200, 300, 300, 0x9d6346, 3)
    MsgBox "A color within 3 shades of variation was found at X" Px " Y" Py
else
    MsgBox "That color was not found in the specified region."
</pre>
</div>

</body>
</html>